import { BaseEnvMirror } from "@/modules/dev-env/mirror/mirror.ts";
import type { MirrorWebsiteFile } from "@common/spider-types.ts";
import type { ToolInstallVersionItem } from "@/modules/dev-env/common/types.ts";

/**
 * GraalVM南京大学镜像
 */
export class GraalVMNanjinguMirror extends BaseEnvMirror {
  constructor() {
    super({
      id: "graalvm-ce.nanjing",
      name: "南京大学(GraalVM)",
      url: "https://mirrors.nju.edu.cn/github-release/graalvm/graalvm-ce-builds/LatestRelease/",
      variant: "graalvm",
      website: "https://mirrors.nju.edu.cn/",
    });
  }

  protected getDownloadList0(files: MirrorWebsiteFile[]): ToolInstallVersionItem[] {
    const platform = this._getPlatform();
    if (!platform) return [];
    return files
      .filter(it => it.name.includes(platform) && it.name.endsWith(".zip"))
      .map(it => ({
        id: `graalvm-ce-${it.name}`,
        name: it.name,
        url: `${this.url}/${it.name}`,
        version: it.name.match(/\d+\.\d+(\.\d+)?/)?.[0] || "",
        edition: "graalvm-ce",
      }));
  }

  private _getPlatform() {
    switch (native.process.platform) {
      case "darwin":
        return "macos";
      case "linux":
        return "linux";
      case "win32":
        return "windows";
      default:
        return undefined;
    }
  }
}
